package cn.piesat.client.service.impl;

import cn.piesat.client.constant.consist.AppProperties;
import cn.piesat.client.model.dto.base.algorithm.DMSClientBean;
import cn.piesat.client.service.SateService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Async("clientStatusThreadPool")
@Service
public class SateServiceImpl implements SateService {

    private Logger logger = LoggerFactory.getLogger(SateServiceImpl.class);

    @Autowired
    private AppProperties appProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void pushNewAlgorithmInfo(DMSClientBean algorithmBean){
        if(algorithmBean!=null){
            HashOperations stringObjectObjectHashOperations = redisTemplate.opsForHash();
            String keyName = String.format("algorithm:%s:%s:%s", algorithmBean.getAlgorithmBean().getParentSubcategory(),  algorithmBean.getAlgorithmBean().getSubcategory(),algorithmBean.getAlgorithmBean().getAlgorithmIdEng());
            String field = String.format("%s:%s",appProperties.getIp(),appProperties.getPort());
            stringObjectObjectHashOperations.put(keyName,field, JSON.toJSONString(algorithmBean));
            logger.info("# [存储成功!] ------{}算法redis记录成功!------",algorithmBean.getAlgorithmBean().getAlgorithmIdChn());
        }
    }

}
